﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ThresholdAssistant.Autofac;
using ThresholdAssistant.DataContract.Result;
using ThresholdAssistant.Tools.Extensions;
using ThresholdAssistant.WebAPI.Models;
using ThresholdAssistant.WebAPI.Repository;

namespace ThresholdAssistant.WebAPI.Service
{
    /// <summary>
    /// 阈值服务
    /// </summary>
    public class ThresholdService : BaseService, IBaseAutofac
    {
        /// <summary>
        /// 阈值仓储
        /// </summary>
        public ThresholdRepository ThresholdRepository { get; set; }

        /// <summary>
        /// 阈值明细仓储
        /// </summary>
        public ThresholdDetailRepository ThresholdDetailRepository { get; set; }

        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public async Task<ContractResult<IEnumerable<ThresholdData>>> GetListAsync(GetThresholdListParam param)
        {
            var result = new ContractResult<IEnumerable<ThresholdData>>();
            var list = await ThresholdRepository.GetListByGroupIdAsync(param.GroupId);
            result.Data = list?.Select(x => new ThresholdData
            {
                Id = x.Id,
                GroupId = x.GroupId,
                Name = x.Name,
                ThresholdType = x.ThresholdType,
                StartValue = x.StartValue,
                EndValue = x.EndValue,
                Remark = x.Remark
            })?.ToList();
            return result.SetSuceccful();
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public async Task<ContractResult<long>> AddAsync(AddThresholdParam param)
        {
            var result = new ContractResult<long>();
            var id = await ThresholdRepository.AddAsync(new Threshold
            {
                GroupId = param.GroupId,
                Name = param.Name,
                ThresholdType = param.ThresholdType,
                StartValue = param.StartValue,
                EndValue = param.EndValue,
                CreateId = param.MemberId,
                CreateTime = DateTime.Now,
                UpdateTime = DateTime.Now,
                Remark = ""
            });

            //明细
            await ThresholdDetailRepository.DeleteByThresholdIdAsync(id);
            await ThresholdDetailRepository.AddListAsync(param.DetailList?.Select(e => new ThresholdDetail
            {
                ThresholdId = id,
                DataId = e.DataId
            }));
            return result.SetSuceccful(id);
        }

        /// <summary>
        /// 单个
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public async Task<ContractResult<ThresholdData>> GetInfoAsync(long id)
        {
            var result = new ContractResult<ThresholdData>();
            var model = await ThresholdRepository.GetInfoAsync(id);
            if (model != null)
            {
                result.Data = new ThresholdData
                {
                    Id = model.Id,
                    GroupId = model.GroupId,
                    Name = model.Name,
                    ThresholdType = model.ThresholdType,
                    StartValue = model.StartValue,
                    EndValue = model.EndValue,
                    Remark = model.Remark
                };

                //明细
                var details = await ThresholdDetailRepository.GetListAsync(model.Id);
                if (details != null)
                {
                    result.Data.Details = details.ToList();
                }
            }
            return result.SetSuceccful();
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public async Task<ContractResult> UpdateAsync(UpdateThresholdParam param)
        {
            var result = new ContractResult();
            var thresholdInfo = await ThresholdRepository.GetInfoAsync(param.Id);
            if (thresholdInfo != null)
            {
                thresholdInfo.StartValue = param.StartValue;
                thresholdInfo.EndValue = param.EndValue;
                thresholdInfo.ThresholdType = param.ThresholdType;
                thresholdInfo.Name = param.Name;
                thresholdInfo.UpdateTime = DateTime.Now;
                await ThresholdRepository.UpdateAsync(thresholdInfo);
            }

            //明细
            await ThresholdDetailRepository.DeleteByThresholdIdAsync(thresholdInfo.Id);
            await ThresholdDetailRepository.AddListAsync(param.DetailList?.Select(e => new ThresholdDetail
            {
                ThresholdId = param.Id,
                DataId = e.DataId
            }));

            return result.SetSuceccful();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ContractResult> DeleteAsync(long id)
        {
            var result = new ContractResult();
            //仓储职责规范
            await ThresholdRepository.DeleteAsync(id);
            await ThresholdDetailRepository.DeleteByThresholdIdAsync(id);
            return result.SetSuceccful();
        }

        
    }
}
